
#include "util/udp_receiver.h"
#include "util/udp_multicast.h"

#define MULTICASTIP "224.0.0.1"
#define CLIENT_MULTICASTPORT 3232



void* clientReceiverThread(void *arg) 
{
	udp_receiver receiver(MULTICASTIP, CLIENT_MULTICASTPORT);

        while(1) 
	{
                std::string ip;
                server::Block ok(receiver.getBlock(ip));
        
                std::cout << ok.master().time() << std::endl;
        }
        return NULL;
}

void* masterSendThread(void *arg) 
{

	std::string nic("eth0");
	udp_multicast sender(MULTICASTIP, CLIENT_MULTICASTPORT, nic);

        while(1) {

                server::Block block;
                unsigned int t = static_cast<unsigned int>(time(NULL));
                block.mutable_master()->set_time(t);

                sender.send(block);
                sleep(1);
        }
        return NULL;

}


class timeservice
{
	public:
	timeservice()
	{


	}

	void client()
	{


                pthread_create(&(clientProcess), NULL, &clientReceiverThread, NULL);

	}

	void master()
	{

                pthread_create(&(masterProcess), NULL, &masterSendThread, NULL);


	}
	private:
		  pthread_t masterProcess;
		  pthread_t clientProcess;

};
/*
int main()
{

}
*/
